#include "QQVideoView.h"

Q_TAKEOVER_NATIVEEVENT_CPP(QQVideoView, QQVideoView::mAppBar)
QQVideoView::QQVideoView(QWidget* parent)
	: QDialog(parent)
{
	this->setFixedSize(QSize(650, 550));
	this->setWindowTitle("QQAudio");

	mTimer = new QTimer(this);
	mTimer->setInterval(1000);

	mAppBar = new ElaAppBar(this);
	mAppBar->setIsDefaultClosed(false);
	mAppBar->setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
	mAppBar->setIsFixedSize(true);

	mLocal = new QLabel(this);
	mLocal->setFixedSize(QSize(200, 150));
	mLocal->move(this->width() - mLocal->width(), mAppBar->getAppBarHeight());
	mLocal->setStyleSheet("background-color: black;");

	mIconLabel = new QLabel(this);
	mIconLabel->setAlignment(Qt::AlignCenter);

	mStateText = new ElaText(this);
	mStateText->setAlignment(Qt::AlignCenter);
	mStateText->setTextPixelSize(10);
	mStateText->setTextPointSize(10);

	mMicrophoneButton = new QQIconButton(ElaIconType::Microphone, 25, this);
	mMicrophoneButton->setCheckable(true);
	mMicrophoneButton->setChecked(true);
	mMicrophoneButton->setNormalColor(Qt::white);
	QColor color = mMicrophoneButton->getNormalColor();
	int alpha = mMicrophoneButton->getNormalColor().alpha();
	color.setAlpha(0.8 * alpha);
	mMicrophoneButton->setHoverColor(color);
	color.setAlpha(0.6 * alpha);
	mMicrophoneButton->setPressColor(color);
	mMicrophoneButton->setNormalIconColor(Qt::black);
	mMicrophoneButton->setHoverIconColor(Qt::black);
	mMicrophoneButton->setPressIconColor(Qt::black);
	mMicrophoneButton->setFixedSize(QSize(50, 50));
	mMicrophoneButton->setBorderRadius(5);

	mAcceptButton = new QQIconButton(ElaIconType::Phone, 23, this);
	mAcceptButton->setNormalColor(Qt::blue);
	color = mAcceptButton->getNormalColor();
	alpha = color.alpha();
	color.setAlpha(0.8 * alpha);
	mAcceptButton->setHoverColor(color);
	color.setAlpha(0.6 * alpha);
	mAcceptButton->setPressColor(color);
	mAcceptButton->setNormalIconColor(Qt::white);
	mAcceptButton->setHoverIconColor(Qt::white);
	mAcceptButton->setPressIconColor(Qt::white);
	mAcceptButton->setFixedSize(QSize(50, 50));
	mAcceptButton->setBorderRadius(5);

	mRefuseButton = new QQIconButton(ElaIconType::PhoneHangup, 25, this);
	mRefuseButton->setNormalColor(Qt::red);
	color = mRefuseButton->getNormalColor();
	alpha = color.alpha();
	color.setAlpha(0.8 * alpha);
	mRefuseButton->setHoverColor(color);
	color.setAlpha(0.6 * alpha);
	mRefuseButton->setPressColor(color);
	mRefuseButton->setNormalIconColor(Qt::white);
	mRefuseButton->setHoverIconColor(Qt::white);
	mRefuseButton->setPressIconColor(Qt::white);
	mRefuseButton->setFixedSize(QSize(50, 50));
	mRefuseButton->setBorderRadius(5);

	mCameraButton = new QQIconButton(ElaIconType::Video, 25, this);
	mCameraButton->setCheckable(true);
	mCameraButton->setChecked(true);
	mCameraButton->setNormalColor(Qt::white);
	color = mCameraButton->getNormalColor();
	alpha = color.alpha();
	color.setAlpha(0.8 * alpha);
	mCameraButton->setHoverColor(color);
	color.setAlpha(0.6 * alpha);
	mCameraButton->setPressColor(color);
	mCameraButton->setNormalIconColor(Qt::black);
	mCameraButton->setHoverIconColor(Qt::black);
	mCameraButton->setPressIconColor(Qt::black);
	mCameraButton->setFixedSize(QSize(50, 50));
	mCameraButton->setBorderRadius(5);

	QHBoxLayout* buttonLayout = new QHBoxLayout;
	buttonLayout->setContentsMargins(0, 0, 0, 0);
	buttonLayout->setSpacing(0);
	buttonLayout->addWidget(mMicrophoneButton);
	buttonLayout->addWidget(mAcceptButton);
	buttonLayout->addWidget(mRefuseButton);
	buttonLayout->addWidget(mCameraButton);

	QVBoxLayout* layout = new QVBoxLayout;
	layout->setContentsMargins(0, 0, 0, 40);
	layout->setSpacing(20);
	layout->addStretch();
	layout->addWidget(mIconLabel);
	layout->addStretch();
	layout->addWidget(mStateText);
	layout->addLayout(buttonLayout);

	this->setLayout(layout);

	connect(mTimer, &QTimer::timeout, this, &QQVideoView::doTimeOut);
	connect(mMicrophoneButton, &QQIconButton::toggled, this, &QQVideoView::doMicrophoneClick);
	connect(mCameraButton, &QQIconButton::toggled, this, &QQVideoView::doCameraClick);
	connect(mAcceptButton, &QQIconButton::clicked, this, &QQVideoView::doAcceptClick);
	connect(mRefuseButton, &QQIconButton::clicked, this, &QQVideoView::doCloseClick);
	connect(mAppBar, &ElaAppBar::closeButtonClicked, this, &QQVideoView::doCloseClick);
}

QQVideoView::~QQVideoView()
{
}

void QQVideoView::changeMicrophoneState(bool opened)
{
	mMicrophoneButton->setChecked(opened);
	auto icon = opened ? ElaIconType::Microphone : ElaIconType::MicrophoneSlash;
	mMicrophoneButton->setAwesome(icon);
}

void QQVideoView::setTargetUserInfo(const QString& account, const QString& nickname, QPixmap pixmap)
{
	mAccount = account;
	mIconLabel->setPixmap(pixmap);
	mStateText->setText(QString("Waiting for %1 to answer").arg(nickname));

	QImage image = pixmap.toImage();
	QGraphicsBlurEffect* blur = new QGraphicsBlurEffect(this);
	blur->setBlurRadius(15);
	image = QQSystemApi::applyEffectToImage(image, blur);
	mBackground = QPixmap::fromImage(image);
}

QPair<QWidget*, QWidget*> QQVideoView::startRealCommunicateVideo()
{
	if (mAcceptButton->isVisible())
		mAcceptButton->hide();
	mTimer->start();
	mIconLabel->setVisible(false);
	mTimeCountSeconds = 0;
	return { this, mLocal };
}

void QQVideoView::stopRealCommunicateVideo(QQVideoView::StopType type)
{
	switch (type)
	{
	case QQVideoView::StopType::Refused:
	{
		mTimer->stop();
		mStateText->setText("The other party has refused");
		this->close();
		break;
	}
	case QQVideoView::StopType::Dropped:
	{
		mTimer->stop();
		mStateText->setText("The other party has finished");
		this->close();
		break;
	}
	default:
		break;
	}
}

void QQVideoView::openThisInSender()
{
	mIconLabel->setVisible(true);
	mAcceptButton->hide();
	this->show();
}

void QQVideoView::openThisInReceiver()
{
	mIconLabel->setVisible(true);
	this->show();
}

void QQVideoView::setBackground(const QPixmap& pixmap)
{
	mBackground = pixmap;
	this->update();
}

void QQVideoView::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	painter.drawPixmap(0, 0, this->width(), this->height(), mBackground);
	return QWidget::paintEvent(event);
}

void QQVideoView::doTimeOut()
{
	mTimeCountSeconds++;
	QTime time(0, 0, 0);
	mStateText->setText(time.addSecs(mTimeCountSeconds).toString("hh:mm:ss"));
}

void QQVideoView::doMicrophoneClick(bool checked)
{
	mMicrophoneButton->setAwesome(checked ? ElaIconType::Microphone : ElaIconType::MicrophoneSlash);
	emit microphone(checked);
}

void QQVideoView::doCameraClick(bool checked)
{
	mCameraButton->setAwesome(checked ? ElaIconType::Video : ElaIconType::VideoSlash);
	emit camera(checked);
}

void QQVideoView::doAcceptClick()
{
	mAcceptButton->hide();
	emit accept(mAccount);
}

void QQVideoView::doCloseClick()
{
	if (mTimer->isActive())
		mTimer->stop();
	emit refuse(mAccount);
	this->close();
}